package com.hhss.qishi.mapper.system.provider;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import com.hhss.qishi.entity.system.User;

/**
 * 用户
 * sql构造器
 * @author hhss
 * @date 2019年4月14日 
 */
public class UserProvider {
	
	private static String SQL_ENTITY = "id, uuid, user_name as userName, password, login_id as loginId, gender, "
			+ "email, phone, salt, spec, status, "
			+ "create_user AS createUser, create_time AS createTime, last_update_user AS lastUpdateUser, "
			+ "last_update_time AS lastUpdateTime, create_user AS createUser, last_update_user AS lastUpdateUser";
	
	private static String TABLE_NAME = "sys_user";
	
	/**
	 * 查找用户集合
	 * @param user
	 * @return
	 */
	public String getListSQL(final User user) {
		return new SQL(){{
			 SELECT(SQL_ENTITY);
			 FROM(TABLE_NAME);
			 WHERE("1=1");
			 if(!StringUtils.isEmpty(user.getUserName())) {
				 AND().WHERE("user_name like CONCAT('%',#{user.userName},'%')");
			 }
			 if(!StringUtils.isEmpty(user.getLoginId())) {
				 AND().WHERE("login_id like CONCAT('%',#{user.loginId},'%')");
			 }
			 if(!StringUtils.isEmpty(user.getCreateUser())) {
				 AND().WHERE("create_user = #{user.createUser}");
			 }
			 if(!StringUtils.isEmpty(user.getEmail())) {
				 AND().WHERE("email like CONCAT('%',#{user.email},'%')");
			 }
			 if(!StringUtils.isEmpty(user.getPhone())) {
				 AND().WHERE("phone like CONCAT('%',#{user.phone},'%')");
			 }
			 if(!StringUtils.isEmpty(user.getStatus())) {
				 AND().WHERE("status = #{user.status}");
			 }
			 if(!StringUtils.isEmpty(user.getCreateUser())) {
				 AND().WHERE("create_user = #{user.createUser}");
			 }
			 if(!StringUtils.isEmpty(user.getCreateTime())) {
				 AND().WHERE("create_time = #{user.createTime}");
			 }
			 if(!StringUtils.isEmpty(user.getLastUpdateUser())) {
				 AND().WHERE("last_update_user = #{user.lastUpdateUser}");
			 }
			 if(!StringUtils.isEmpty(user.getLastUpdateTime())) {
				 AND().WHERE("last_update_time = #{user.lastUpdateTime}");
			 }
			 
		 }}.toString();
	}
	
	public String getEntitySQL(final User user) {
		return new SQL(){{
			SELECT(SQL_ENTITY);
			 FROM(TABLE_NAME);
			 WHERE("1=1");
			 if(!StringUtils.isEmpty(user.getId())) {
				 AND().WHERE("id = #{user.id}");
			 }
			 if(!StringUtils.isEmpty(user.getUuid())) {
				 AND().WHERE("uuid = #{user.uuid}");
			 }
			 if(!StringUtils.isEmpty(user.getLoginId())) {
				 AND().WHERE("login_id = #{user.loginId}");
			 }
		}}.toString();
	}
}
